package com.xjie.mall.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjie.mall.goods.mapper.BrandMapper;
import com.xjie.mall.goods.mapper.CategoryMapper;
import com.xjie.mall.goods.mapper.SkuMapper;
import com.xjie.mall.goods.mapper.SpuMapper;
import com.xjie.mall.goods.model.*;
import com.xjie.mall.goods.service.SpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 商品SPU表 服务实现类
 * </p>
 *
 * @author xjie
 * @since 2025-10-17
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private SpuMapper spuMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BrandMapper brandMapper;
    @Override
    public void saveProduct(Product product) {

        //Spu
        Spu spu = product.getSpu();

        //判断是新增方法还是修改方法
        //若spu的id为空,则新增
        if(StringUtils.isEmpty(spu.getId()))
        {
            //是否上架(0已下架,1已上架)
            spu.setIsMarketable(1);

            //是否删除(0:未删除,1:已删除)
            spu.setIsDelete(0);

            //审核状态(0:未审核,1:已审核,2:审核不通过)
            spu.setStatus(1);

            //添加此Spu
            spuMapper.insert(spu);
        }else
        {
            //id不为空,则修改
            spuMapper.updateById(spu);
            //删除之前的Sku记录
            skuMapper.delete(new QueryWrapper<Sku>().eq("spu_id",spu.getId()));

        }



        //查询三级分类
        Category category = categoryMapper.selectById(spu.getCategoryThreeId());

        //查询品牌
        Brand brand = brandMapper.selectById(spu.getBrandId());



        //新增Sku集合
        for(Sku sku : product.getSkus())
        {
            //设置名字
            String skuName = spu.getName();
            Map<String,String> attrMap = JSON.parseObject(sku.getSkuAttribute(), Map.class);
            for(Map.Entry<String,String> entry : attrMap.entrySet())
            {
                skuName += "   " + entry.getValue();
            }
            sku.setName(skuName);

            //设置图片
            sku.setImage(spu.getImages());

            //设置状态
            sku.setStatus(1);
            //设置类目ID
            sku.setCategoryId(spu.getCategoryThreeId());
            //设置类目名称
            sku.setCategoryName(category.getName());
            //设置品牌ID
            sku.setBrandId(brand.getId());
            //设置品牌名称
            sku.setBrandName(brand.getName());
            //设置Spuid
            sku.setSpuId(spu.getId());
            //时间
            sku.setCreateTime(LocalDateTime.now());
            sku.setUpdateTime(LocalDateTime.now());
            //增加
            skuMapper.insert(sku);
        }

    }
}
